﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
namespace NetTao.Config
{
    public class RuleManage
    {
        private static RuleManage _ruleManage = null;

        public static RuleManage Instace()
        {
            return _ruleManage ?? (_ruleManage = new RuleManage());
        }

        public RuleEntity GetRuleFormXml(string xmlPath)
        {
            if (xmlPath == null) throw new ArgumentNullException("xmlPath");
            using (var ds = new DataSet())
            {
                var rule = new RuleEntity();
                ds.ReadXml(xmlPath);
                #region 实体类赋值

                RulePara rulePara = GetRuleParaFormTable(ds.Tables["GetSiteCharset"]);
                rule.GetSiteCharset.RegexName = rulePara.RegexName;
                rule.GetSiteCharset.Pattern = rulePara.Pattern;
                rule.GetSiteCharset.Method = rulePara.Method;
                rule.GetSiteCharset.Options = rulePara.Options;
                rule.GetSiteCharset.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["GetSiteName"]);
                rule.GetSiteName.RegexName = rulePara.RegexName;
                rule.GetSiteName.Pattern = rulePara.Pattern;
                rule.GetSiteName.Method = rulePara.Method;
                rule.GetSiteName.Options = rulePara.Options;
                rule.GetSiteName.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["GetSiteUrl"]);
                rule.GetSiteUrl.RegexName = rulePara.RegexName;
                rule.GetSiteUrl.Pattern = rulePara.Pattern;
                rule.GetSiteUrl.Method = rulePara.Method;
                rule.GetSiteUrl.Options = rulePara.Options;
                rule.GetSiteUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["LagerSort"]);
                rule.LagerSort.RegexName = rulePara.RegexName;
                rule.LagerSort.Pattern = rulePara.Pattern;
                rule.LagerSort.Method = rulePara.Method;
                rule.LagerSort.Options = rulePara.Options;
                rule.LagerSort.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelAuthor"]);
                rule.NovelAuthor.RegexName = rulePara.RegexName;
                rule.NovelAuthor.Pattern = rulePara.Pattern;
                rule.NovelAuthor.Method = rulePara.Method;
                rule.NovelAuthor.Options = rulePara.Options;
                rule.NovelAuthor.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubIndexUrl"]);
                rule.PubIndexUrl.RegexName = rulePara.RegexName;
                rule.PubIndexUrl.Pattern = rulePara.Pattern;
                rule.PubIndexUrl.Method = rulePara.Method;
                rule.PubIndexUrl.Options = rulePara.Options;
                rule.PubIndexUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelCover"]);
                rule.NovelCover.RegexName = rulePara.RegexName;
                rule.NovelCover.Pattern = rulePara.Pattern;
                rule.NovelCover.Method = rulePara.Method;
                rule.NovelCover.Options = rulePara.Options;
                rule.NovelCover.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelDefaultCoverUrl"]);
                rule.NovelDefaultCoverUrl.RegexName = rulePara.RegexName;
                rule.NovelDefaultCoverUrl.Pattern = rulePara.Pattern;
                rule.NovelDefaultCoverUrl.Method = rulePara.Method;
                rule.NovelDefaultCoverUrl.Options = rulePara.Options;
                rule.NovelDefaultCoverUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelDegree"]);
                rule.NovelDegree.RegexName = rulePara.RegexName;
                rule.NovelDegree.Pattern = rulePara.Pattern;
                rule.NovelDegree.Method = rulePara.Method;
                rule.NovelDegree.Options = rulePara.Options;
                rule.NovelDegree.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelErr"]);
                rule.NovelErr.RegexName = rulePara.RegexName;
                rule.NovelErr.Pattern = rulePara.Pattern;
                rule.NovelErr.Method = rulePara.Method;
                rule.NovelErr.Options = rulePara.Options;
                rule.NovelErr.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelInfo_GetNovelPubKey"]);
                rule.NovelInfo_GetNovelPubKey.RegexName = rulePara.RegexName;
                rule.NovelInfo_GetNovelPubKey.Pattern = rulePara.Pattern;
                rule.NovelInfo_GetNovelPubKey.Method = rulePara.Method;
                rule.NovelInfo_GetNovelPubKey.Options = rulePara.Options;
                rule.NovelInfo_GetNovelPubKey.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelIntro"]);
                rule.NovelIntro.RegexName = rulePara.RegexName;
                rule.NovelIntro.Pattern = rulePara.Pattern;
                rule.NovelIntro.Method = rulePara.Method;
                rule.NovelIntro.Options = rulePara.Options;
                rule.NovelIntro.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelKeyword"]);
                rule.NovelKeyword.RegexName = rulePara.RegexName;
                rule.NovelKeyword.Pattern = rulePara.Pattern;
                rule.NovelKeyword.Method = rulePara.Method;
                rule.NovelKeyword.Options = rulePara.Options;
                rule.NovelKeyword.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelList_GetNovelKey"]);
                rule.NovelList_GetNovelKey.RegexName = rulePara.RegexName;
                rule.NovelList_GetNovelKey.Pattern = rulePara.Pattern;
                rule.NovelList_GetNovelKey.Method = rulePara.Method;
                rule.NovelList_GetNovelKey.Options = rulePara.Options;
                rule.NovelList_GetNovelKey.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelListUrl"]);
                rule.NovelListUrl.RegexName = rulePara.RegexName;
                rule.NovelListUrl.Pattern = rulePara.Pattern;
                rule.NovelListUrl.Method = rulePara.Method;
                rule.NovelListUrl.Options = rulePara.Options;
                rule.NovelListUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelName"]);
                rule.NovelName.RegexName = rulePara.RegexName;
                rule.NovelName.Pattern = rulePara.Pattern;
                rule.NovelName.Method = rulePara.Method;
                rule.NovelName.Options = rulePara.Options;
                rule.NovelName.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelSearch_GetNovelKey"]);
                rule.NovelSearch_GetNovelKey.RegexName = rulePara.RegexName;
                rule.NovelSearch_GetNovelKey.Pattern = rulePara.Pattern;
                rule.NovelSearch_GetNovelKey.Method = rulePara.Method;
                rule.NovelSearch_GetNovelKey.Options = rulePara.Options;
                rule.NovelSearch_GetNovelKey.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelSearchData"]);
                rule.NovelSearchData.RegexName = rulePara.RegexName;
                rule.NovelSearchData.Pattern = rulePara.Pattern;
                rule.NovelSearchData.Method = rulePara.Method;
                rule.NovelSearchData.Options = rulePara.Options;
                rule.NovelSearchData.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelSearchUrl"]);
                rule.NovelSearchUrl.RegexName = rulePara.RegexName;
                rule.NovelSearchUrl.Pattern = rulePara.Pattern;
                rule.NovelSearchUrl.Method = rulePara.Method;
                rule.NovelSearchUrl.Options = rulePara.Options;
                rule.NovelSearchUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["NovelUrl"]);
                rule.NovelUrl.RegexName = rulePara.RegexName;
                rule.NovelUrl.Pattern = rulePara.Pattern;
                rule.NovelUrl.Method = rulePara.Method;
                rule.NovelUrl.Options = rulePara.Options;
                rule.NovelUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubChapter_GetChapterKey"]);
                rule.PubChapter_GetChapterKey.RegexName = rulePara.RegexName;
                rule.PubChapter_GetChapterKey.Pattern = rulePara.Pattern;
                rule.PubChapter_GetChapterKey.Method = rulePara.Method;
                rule.PubChapter_GetChapterKey.Options = rulePara.Options;
                rule.PubChapter_GetChapterKey.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubChapterName"]);
                rule.PubChapterName.RegexName = rulePara.RegexName;
                rule.PubChapterName.Pattern = rulePara.Pattern;
                rule.PubChapterName.Method = rulePara.Method;
                rule.PubChapterName.Options = rulePara.Options;
                rule.PubChapterName.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubContent_GetTextKey"]);
                rule.PubContent_GetTextKey.RegexName = rulePara.RegexName;
                rule.PubContent_GetTextKey.Pattern = rulePara.Pattern;
                rule.PubContent_GetTextKey.Method = rulePara.Method;
                rule.PubContent_GetTextKey.Options = rulePara.Options;
                rule.PubContent_GetTextKey.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubContentErr"]);
                rule.PubContentErr.RegexName = rulePara.RegexName;
                rule.PubContentErr.Pattern = rulePara.Pattern;
                rule.PubContentErr.Method = rulePara.Method;
                rule.PubContentErr.Options = rulePara.Options;
                rule.PubContentErr.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubContentImages"]);
                rule.PubContentImages.RegexName = rulePara.RegexName;
                rule.PubContentImages.Pattern = rulePara.Pattern;
                rule.PubContentImages.Method = rulePara.Method;
                rule.PubContentImages.Options = rulePara.Options;
                rule.PubContentImages.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubContentReplace"]);
                rule.PubContentReplace.RegexName = rulePara.RegexName;
                rule.PubContentReplace.Pattern = rulePara.Pattern;
                rule.PubContentReplace.Method = rulePara.Method;
                rule.PubContentReplace.Options = rulePara.Options;
                rule.PubContentReplace.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubContentText"]);
                rule.PubContentText.RegexName = rulePara.RegexName;
                rule.PubContentText.Pattern = rulePara.Pattern;
                rule.PubContentText.Method = rulePara.Method;
                rule.PubContentText.Options = rulePara.Options;
                rule.PubContentText.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubContentUrl"]);
                rule.PubContentUrl.RegexName = rulePara.RegexName;
                rule.PubContentUrl.Pattern = rulePara.Pattern;
                rule.PubContentUrl.Method = rulePara.Method;
                rule.PubContentUrl.Options = rulePara.Options;
                rule.PubContentUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubCookies"]);
                rule.PubCookies.RegexName = rulePara.RegexName;
                rule.PubCookies.Pattern = rulePara.Pattern;
                rule.PubCookies.Method = rulePara.Method;
                rule.PubCookies.Options = rulePara.Options;
                rule.PubCookies.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubIndexErr"]);
                rule.PubIndexErr.RegexName = rulePara.RegexName;
                rule.PubIndexErr.Pattern = rulePara.Pattern;
                rule.PubIndexErr.Method = rulePara.Method;
                rule.PubIndexErr.Options = rulePara.Options;
                rule.PubIndexErr.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubTextUrl"]);
                rule.PubTextUrl.RegexName = rulePara.RegexName;
                rule.PubTextUrl.Pattern = rulePara.Pattern;
                rule.PubTextUrl.Method = rulePara.Method;
                rule.PubTextUrl.Options = rulePara.Options;
                rule.PubTextUrl.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubVolumeContent"]);
                rule.PubVolumeContent.RegexName = rulePara.RegexName;
                rule.PubVolumeContent.Pattern = rulePara.Pattern;
                rule.PubVolumeContent.Method = rulePara.Method;
                rule.PubVolumeContent.Options = rulePara.Options;
                rule.PubVolumeContent.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubVolumeName"]);
                rule.PubVolumeName.RegexName = rulePara.RegexName;
                rule.PubVolumeName.Pattern = rulePara.Pattern;
                rule.PubVolumeName.Method = rulePara.Method;
                rule.PubVolumeName.Options = rulePara.Options;
                rule.PubVolumeName.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["PubVolumeSplit"]);
                rule.PubVolumeSplit.RegexName = rulePara.RegexName;
                rule.PubVolumeSplit.Pattern = rulePara.Pattern;
                rule.PubVolumeSplit.Method = rulePara.Method;
                rule.PubVolumeSplit.Options = rulePara.Options;
                rule.PubVolumeSplit.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["RuleID"]);
                rule.RuleID.RegexName = rulePara.RegexName;
                rule.RuleID.Pattern = rulePara.Pattern;
                rule.RuleID.Method = rulePara.Method;
                rule.RuleID.Options = rulePara.Options;
                rule.RuleID.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["RuleVersion"]);
                rule.RuleVersion.RegexName = rulePara.RegexName;
                rule.RuleVersion.Pattern = rulePara.Pattern;
                rule.RuleVersion.Method = rulePara.Method;
                rule.RuleVersion.Options = rulePara.Options;
                rule.RuleVersion.FilterPattern = rulePara.FilterPattern;

                rulePara = GetRuleParaFormTable(ds.Tables["SmallSort"]);
                rule.SmallSort.RegexName = rulePara.RegexName;
                rule.SmallSort.Pattern = rulePara.Pattern;
                rule.SmallSort.Method = rulePara.Method;
                rule.SmallSort.Options = rulePara.Options;
                rule.SmallSort.FilterPattern = rulePara.FilterPattern;

                //rulePara = GetRuleParaFormTable(ds.Tables["NovelFullText"]);
                //rule.NovelFullText.RegexName = rulePara.RegexName;
                //rule.NovelFullText.Pattern = rulePara.Pattern;
                //rule.NovelFullText.Method = rulePara.Method;
                //rule.NovelFullText.Options = rulePara.Options;
                //rule.NovelFullText.FilterPattern = rulePara.FilterPattern;

                #endregion
                return rule;
            }
        }

        /// <summary>
        /// 根据DATATABLE取得参数值
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private RulePara GetRuleParaFormTable(DataTable dt)
        {
            RulePara para = new RulePara();
            para.RegexName = dt.Rows[0]["RegexName"].ToString();
            para.Pattern = dt.Rows[0]["Pattern"].ToString();
            para.Method = dt.Rows[0]["Method"].ToString();
            if (dt.Rows[0]["Options"].ToString().Contains("Compiled"))
            {
                para.Options &= RegexOptions.Compiled;
            }
            if (dt.Rows[0]["Options"].ToString().Contains("IgnoreCase"))
            {
                para.Options &= RegexOptions.IgnoreCase;
            }
            if (dt.Rows[0]["Options"].ToString().Contains("IgnorePatternWhitespace"))
            {
                para.Options &= RegexOptions.IgnorePatternWhitespace;
            }
            if (dt.Rows[0]["Options"].ToString().Contains("Multiline"))
            {
                para.Options &= RegexOptions.Multiline;
            }
            if (dt.Rows[0]["Options"].ToString().Contains("Singleline"))
            {
                para.Options &= RegexOptions.Singleline;
            }
            if (dt.Rows[0]["Options"].ToString().Contains("RightToLeft"))
            {
                para.Options &= RegexOptions.RightToLeft;
            }
            para.FilterPattern = dt.Rows[0]["FilterPattern"].ToString();
            return para;
        }

    }
}
